<?php 
session_start();

function msg($msg, $url=''){
		if($url == ''){
			$ad = "window.history.go(-1)";
		}else{
			$ad = "window.location.href='{$url}'";
		}
		exit("<script>alert('{$msg}');{$ad};</script>");
	}

	include_once("./head.php");
	include_once("./commonality.php");
	$ser = "select * from ts_address";
	$tre = mysql_query($ser);
	while ($res = mysql_fetch_assoc($tre)) {
	    $setr[] = $res;
	}

	$set="select * from product";
	$res=mysql_query($set);
	while($list=mysql_fetch_assoc($res)){
	$sets[]=$list;
	}
	if(isset($_POST['sub'])){
		//获取数据
		$data = $_POST;

		//处理数据
		$data['uid'] = $_SESSION['uid'];
		unset($data['sub']);

if (empty($_POST['t_name'])) {
		msg('收货人不能为空');
	}
	if (empty($_POST['t_add'])) {
		msg('收货人地址不能为空');
	}
	if (empty($_POST['phone'])) {
		msg('收货人联系方式不能为空');
	}

		//组装sql语句并执行
	$sql = "insert into ts_address set ";
	foreach($data as $k=>$value) {
		$sql.= "{$k}='{$value}',";		
	}
		$sql = substr($sql, 0, -1);
		mysql_query($sql);
		
		//提示
	if(mysql_affected_rows()>0){
 		echo "<script>alert('新增收货地址成功');location.href='close.php';</script>";
 	}else{
 		//echo "<script>alert('新增收货地址失败');location.href='close.php';</script>";
 	}
}



 ?>
<!DOCTYPE html>
<html>
<head>
	<title>结算</title>
</head>
	<style type="text/css">
		.c1{
			width:10%;
			height:40px;
			/*border:1px solid red;*/
			margin-left:80px;
			font-size:18px;

		}
	.indent{
			/*border:1px solid red;*/
			margin-left:80px;
			font-size:18px;

	}
	.inpu{
		width:300px;
		height:30px;
		border:2px solid #31CBE3;
		border-radius:5px;
		box-shadow:0px 0px 12px #BFBFBF;
	}
	.baocun{
			width:100px;
			height:40px;
			/*border:1px solid red;*/
			margin-left:160px;
			background:#66D9EF;
			font-size:20px;
			color:white;
		}
	.quxiao{
			width:100px;
			height:40px;
			/*border:1px solid red;*/
			margin-left:80px;
			background:#9D9DA2;
			font-size:20px;
			color:white;
		}
	.color5{
		width:30px;
		height:30px;
		display:inline-block;
	}
	.close{
		width:160px;
		height:50px;
		background:#2894ff;
		display:inline-block;
		margin-top:30px;
		margin-left:1000px;
		margin-right:100px;
		line-height:50px;
		text-align:center;
		color:white;
		font-size:20px;
		border-radius:4px;
	}
	.close1{
		width:260px;
		height:50px;
		background:#2894ff;
		display:inline-block;
		margin-top:30px;
		margin-left:950px;
		margin-right:100px;
		line-height:50px;
		text-align:center;
		color:white;
		font-size:20px;
		border-radius:4px;
	}
	</style>
<body>
	<span class="c1">当前所在：商城>订单</span><hr color="#d1d1d1"><br><br>
	<form action="" method="post">
	<table class="indent" cellspacing="20px">
		<tr><td>新增收货地址<td></tr>
		<tr>			
			<td>收货人:</td>
			<td><input type="text" name="t_name" class="inpu"></td>		
		</tr>
		<tr>			
			<td>详细地址:</td>
			<td><input type="text" name="t_add" class="inpu"></td>			
		</tr>
		<tr>			
			<td>联系方式:</td>
			<td><input type="text" name="phone" class="inpu" id="phone"></td>	
		</tr>
		<tr>
			<td><input type="checkbox" name="addr_default" value="1">设置默认地址</td>
		</tr>
	</table>
	<div>
		<input type="submit" name="sub" value="保存" class="baocun">
		<input type="submit" name="sub" value="取消" class="quxiao">
	</div>
	</form>

<script>
			
		
			phone=document.getElementById("phone");
			span_h=document.getElementById("zspan2");

		
			p=1;
			p1=0;
			p2=0;
			p3=0;
				
				
			
				phone.onblur=function(){
					phone_h=phone.value
					pergg=/^1[3,5,8,9]{1}\d{9}$/
					if(pergg.test(phone_h)){
						span_h.innerHTML="√";
						span_h.style.color="green";
						p2=1;	
					}else{
						span_h.innerHTML="*请输入正确的手机号码";
						span_h.style.color="red";
						p2=0;
					}
				}
				phone.onfocus=function(){
					span_h.innerHTML="";
				}
				
			</script>
		

	<table cellspacing="20px" class="indent" >		
		<tr>
			<td>收货信息</td>			
		</tr>
<form action="tijiao.php" method="post">
<?php foreach ($setr as $value){?>	
		<tr>
			<td width="800px"><input type="radio" name="addr_id" value="<?= $value['addr_id']?>"  checked>收货人:<?= $value['t_name']?>;收货地址:<?= $value['t_add']?>;手机:<?= $value['phone']?>;</td>	
			<td><a style="text-decoration:none;" href="addbel.php?addr_id=<?= $value['addr_id']?>">删除</a></td>		
		</tr>
	<?php	}?>
	</table>
	<hr color="#d1d1d1" width="90%">
	<table class="indent">
	<tr>
		<td width="200px">图片</td>
		<td width="200px">名称</td>
		<td width="200px">信息</td>
		<td width="200px">价格</td>
		<td width="200px">数量</td>
		<td width="200px">小计</td>
	</tr>
	<?php foreach ($sets as$key=>$value){ 
		foreach ($_SESSION['cart'] as $k=>$v) {
			if($v['pid']==$value['pid']){?>
	<tr>
		<td width="200px"><img src="./uploads/<?= $value['pimg']?>" width="80px"></td>
		<td width="200px"><?= $value['pname']?></td>
		<td width="200px"><?= $v['size']?><br><div class="color5"style="background:<?= $v['color'] ?>"></div></td>
		<td width="200px"><?= $value['price']?></td>
		<td width="200px"><?= $v['num']?></td>

		<td width="200px">
			<?php 
		$xiaoji = $value['price']*$v['num'];
		$zongjia += $xiaoji; 
		echo $xiaoji;
		?>
	</td>
	</tr>
<?php 
		}
	}

}?>
	</table>
	<hr color="#d1d1d1" width="90%">	
	<span class="close1">总价：<?= $zongjia ?></span>	
	<input type="submit" name="sub" value="提交订单" class="close">
	</form>

</body>
</html>